package com.itcoon.cloud.framework.webmvc.exception.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

public interface GlobalExceptionHandler<E extends Exception> {

    Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    boolean support(Exception e);

    void handle(HttpServletResponse response, E e);


    default void loopLog(Throwable cause){
        if(cause instanceof Exception){
            log.error(cause.getLocalizedMessage(), cause);
            Exception exception = (Exception) cause;
            Throwable subCause = exception.getCause();
            if(subCause != null){
                loopLog(subCause);
            }else{
               log.error(Arrays.toString(cause.getStackTrace()));
            }
        }
    }
}
